import $http from "@/services";
import { message } from "antd"
export default {
    state: {
        departmentList: [],
        departmentDetail: null,
        isCollapsable: true,
        isShowModal: false
    },
    // 同步操作
    reducers: {
        setDepartmentList(state, { payload }) {
            return { ...state, departmentList: payload }
        },
        setDepartmentDetail(state, { payload }) {
            return { ...state, departmentDetail: payload }
        },
        setIsCollaspable(state, { payload }) {
            return { ...state, isCollapsable: payload }
        },
        setIsShowModal(state, { payload }) {
            return { ...state, isShowModal: payload }
        }
    },
    effects: {
        // 获取部门列表
        *getDepartmentList({ payload }, { put, call }) {
            const { data } = yield call($http.getDepartmentList);
            yield put({
                type: "setDepartmentList",
                payload: data.list
            })
        },
        // 获取部门详情
        *getDepartmentDetail({ payload }, { put, call }) {
            const { data } = yield call($http.getDepartmetDetail, payload);
            yield put({
                type: "setDepartmentDetail",
                payload: data
            });

            yield put({
                type: "setIsShowModal",
                payload: true
            })
        },
        //添加部门
        *addDepartmentList({ payload }, { put, call }) {
            const { data, code, msg } = yield call($http.addDepartmet, payload);
            yield put({
                type: "getDepartmentList"
            });
            if (data) return message.error(msg);
            message.success(msg);
        },
        //更新部门
        *updateDepartmentDetail({ payload }, { put, call }) {
            const { data, msg } = yield call($http.updateDepartment, payload);
            yield put({
                type: "getDepartmentDetail",
                payload: payload._id
            });
            yield put({
                type: "getDepartmentList"
            })
            message.success(msg);
        },
        *deleteDepartment({ payload }, { put, call }) {
            const { msg, data } = yield call($http.delDepartment, payload);
            console.log(msg)
            console.log(data)
            if (msg === '删除成功') {
                yield put({
                    type: "getDepartmentList"
                })
                message.success(msg);
            } else {
                message.error(msg);
            }
        }
    }
}